﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;

namespace cb0tServer
{
    class PMessages
    {
        private static List<AdminPersonalMessageItem> apm_items = new List<AdminPersonalMessageItem>();

        public static String IsAdminMsg(UserObject userobj)
        {
            foreach (AdminPersonalMessageItem m in apm_items)
            {
                if (m.guid.Equals(userobj.guid) || m.ip.Equals(userobj.physical_ip))
                {
                    return m.msg;
                }
            }

            return null;
        }

        public static void Add(UserObject userobj, String msg)
        {
            foreach (AdminPersonalMessageItem m in apm_items)
            {
                if (m.guid.Equals(userobj.guid) ||
                    m.ip.Equals(userobj.physical_ip))
                {
                    m.msg = msg;
                    return;
                }
            }

            AdminPersonalMessageItem mu = new AdminPersonalMessageItem();
            mu.guid = userobj.guid;
            mu.ip = userobj.physical_ip;
            mu.msg = msg;
            mu.org_msg = (userobj.user_message == null ? "" : userobj.user_message);
            apm_items.Add(mu);
        }

        public static String Remove(UserObject userobj)
        {
            for (int i = 0; i < apm_items.Count; i++)
            {
                if (apm_items[i].ip.Equals(userobj.physical_ip) || apm_items[i].guid.Equals(userobj.guid))
                {
                    String str = apm_items[i].org_msg;
                    apm_items.RemoveAt(i);
                    return str;
                }
            }

            return null;
        }

        public static void Clear()
        {
            apm_items.Clear();
        }
    }

    class AdminPersonalMessageItem
    {
        public IPAddress ip;
        public Guid guid;
        public String msg;
        public String org_msg = "";
    }
}
